<?php
//
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'get_detail';
if($_RQ['op']=='get_detail'){
	$id=intval($_RQ['id']);
	//来自其他插件的购物
	$this->session('fromplugin_name',$_RQ['plugin_name']);
	$this->session('frompid',intval($_RQ['frompid']));
	$store_id=$this->session('store_id');
	if(!$id){
		exi('没有商品ID','error','jy_pingdd_pin/index/');
	}
	$pinggoods=pdo_get('jy_pingdd_pin_goods',array('goodsid'=>$id,'status'=>1,'uniacid'=>$_SESSION['uniacid']));
	$_SESSION['pinggoodsid'] = $pinggoods['id'];
	$_SESSION['goodsid'] = $id;
	if($_RQ['room_id']){//直播端roomid房间跟商品绑定
		$room_goods=$this->session('room_goods')?:array();
		$sale_live=plugin('jy_weishop')->plugin_setting('sale_live');
		$wxgid=$_SESSION['wxapp_id']?:intval($_RQ['wxgid']);
		$live_id=pdo_getcolumn('core_account_wxapp_live',array('wxapp_id'=>$wxgid,'uniacid'=>$_SESSION['uniacid']),'id');
		//兑换本地直播ID
		$roomid=pdo_getcolumn('core_account_wxapp_live_rooms',array('roomid'=>$_RQ['room_id'],'live_id'=>$live_id,'uniacid'=>$_SESSION['uniacid']),'id');
		if($sale_live['getfeetype']==1){
			if(!$room_goods[$id]){//是初次绑定
				$room_goods[$id]=intval($roomid);
				$this->session('room_goods',$room_goods);
			}
		}else{
			$room_goods[$id]=intval($roomid);
			$this->session('room_goods',$room_goods);
		}
		pdo_update('core_account_wxapp_live_rooms',array('fromlook +='=>1),array('id'=>$roomid,'uniacid'=>$_SESSION['uniacid']));
	}
	$goods=pdo_get('jy_weishop_goods',array('id'=>$_SESSION['goodsid'],'status'=>1,'uniacid'=>$_SESSION['uniacid']));
	if(!$goods){
		exi('选择的商品不存在或已下架，请重新选择！','error','/index/');
	}
	$goods['originalprice']=$goods['originalprice']>1?$goods['originalprice']:price($goods['price']*1.6);
	$goods['userbuy'] = 1;
	$goods['levelbuy'] = 1;
	$goods['groupbuy'] = 1;
	//if($_SESSION['os']=='windows'){
	$goods['image']=$goods['image']?tomedia($goods['image']):'';
	$goods['video']=$goods['video']?tomedia($goods['video']):'';
	//}
	$goods['images']=json_decode($goods['images'],true);
	foreach($goods['images'] as &$image){
		$image=tomedia($image);
	}
	if(count($goods['images'])<1){
		$goods['images'][]=tomedia($goods['image']);
	}
	$return['url']=custom_url('jy_pingdd_pin/goods.detail//id='.$id,true);
	$goods['pcatetitle']=pdo_getcolumn('jy_weishop_goods_group',array('id'=>$goods['pcate']),'title');
	$goods['ccatetitle']=pdo_getcolumn('jy_weishop_goods_group',array('id'=>$goods['ccate']),'title');
	$return['cate']=array($goods['pcatetitle'],$goods['ccatetitle']);
	//来自分销MID
	$goods['myshopmid']=intval($_RQ['frommyshop']);
	$jy_commission = check_account_permission('jy_commission');
	if($jy_commission && $goods['myshopmid']){
		$member=pdo_get('core_members',array('id'=>$goods['myshopmid']),array('id','avatar','nickname'));
		$goods['myshop']=pdo_get('jy_commission_shop',array('mid'=>$goods['myshopmid'],'uniacid'=>$_SESSION['uniacid']));
		$goods['myshop']['logo']=$goods['myshop']['logo']?tomedia($goods['myshop']['logo']):tomedia($member['avatar']);
		$goods['myshop']['title']=$goods['myshop']['title']?$goods['myshop']['title']:$member['nickname'].'的小店';
	}
	//来自海报
	$jy_poster = $this->plugin_exists('jy_poster');
	if($jy_poster){
		$goods['isposter']=1;
	}
	//来自多商户店(非下级也可以分销）
	$goods['merchid']=$_RQ['merchid']?intval($_RQ['merchid']):$goods['merchid'];
	$this->session('frommerchid',$goods['merchid']);
	$jy_kefu = check_account_permission('jy_kefu');
	if($jy_kefu && $goods['merchid']){
		$goods['merch']=pdo_get('jy_merch_user',array('id'=>$goods['merchid'],'uniacid'=>$_SESSION['uniacid']));
		$member=pdo_get('core_members',array('id'=>$goods['merch']['mid']),array('id','avatar','nickname'));
		$goods['merch']['logo']=$goods['merch']['logo']?tomedia($goods['merch']['logo']):tomedia($member['avatar']);
		$goods['merch']['merchname']=$goods['merch']['merchname']?$goods['merch']['merchname']:$member['nickname'].'的小店';
		if($goods['merch']['iskefu']){
			$goods['merch']['showkefu']=1;
		}
		if(!$goods['merch']['id']){
			$goods['merch']['merchname']='该商户不存在';
		}
	}
	pdo_update('jy_weishop_goods',array('readnum +='=>1),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	//浏览权限
	$levelinfo = model('jy_weishop/member')->get_level();
	if($goods['showlevels']){
		$fastvalue=substr($goods['showlevels'],0,1);
		if($fastvalue!=';'){
			$goods['showlevels']=';'.$goods['showlevels'];
		}
		$lastvalue=substr($goods['showlevels'],-1,1);
		if($lastvalue!=';'){
			$goods['showlevels']=$goods['showlevels'].';';
		}
		if(strpos($goods['showlevels'],';'.$levelinfo['id'].';')===false){
			exi('您没有权限浏览该商品！','error','index/');
		}
	}	
	$groupinfo =  model('jy_weishop/member')->get_group();
	if($goods['showgroups']){
		$fastvalue=substr($goods['showgroups'],0,1);
		if($fastvalue!=';'){
			$goods['showgroups']=';'.$goods['showgroups'];
		}
		$lastvalue=substr($goods['showgroups'],-1,1);
		if($lastvalue!=';'){
			$goods['showgroups']=$goods['showgroups'].';';
		}
		if(strpos($goods['showgroups'],';'.$groupinfo['id'].';')===false){
			exi('您没有权限浏览该商品！','error','index/');
		}
	}
	
	//更新足迹
	$history=pdo_getcolumn('jy_weishop_member_history',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'goodsid'=>$id),'id');
	if($history){
		pdo_update('jy_weishop_member_history',array('goodsid'=>$id,'createtime'=>time()),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'goodsid'=>$id));
	}else{
		pdo_insert('jy_weishop_member_history',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'goodsid'=>$id,'createtime'=>time()));
	}
	//判断是否收藏：
	$return['islike']=pdo_getcolumn('jy_weishop_member_like',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'goodsid'=>$id),'id');
	//判断买家秀是否开启
	//买家秀
	$buyxiu=plugin('jy_weishop')->plugin_setting('buyxiu');
	foreach($buyxiu['goods_ids'] as $xiuv){
		if($xiuv['id'] == $goods['id']){
			$goods['isbuyxiu']=1;
		}
	}
	if($goods['isbuyxiu'] && $buyxiu['likes']){
		$install = check_account_permission('jy_vote');
		if(!$install){
			$goods['isbuyxiu'] =0;
		}
	}
	$install = check_account_permission('jy_commission');
	if($install){
			$goods['jy_commission'] =1;
	}
	//营销设置
	$saleset=plugin('jy_weishop')->plugin_setting('saleset');
	$member=pdo_get('jy_weishop_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
	$member2=pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_SESSION['uid']),array('avatar','nickname','credit1','credit2'));
	$member=array_merge($member,$member2);
	$goods['canbuy'] = ($goods['status'] == 1) && empty($goods['deleted']) && ($goods['total'] > 0);
	$goods['unit'] = ((empty($goods['unit']) ? '件' : $goods['unit']));
	$goods['subtitle'] = cfc('tools')->substr($goods['subtitle'],0,48);
	if($goods['weight']){
		$goods['weight']=round($goods['weight']/1000,2);
	}
	if (0 < $goods['usermaxbuy']) {
		$order_goodscount = pdo_fetchcolumn('select ifnull(sum(og.total),0)  from ' . tablename('jy_weishop_order_goods') . ' og ' . ' left join ' . tablename('jy_weishop_order') . ' o on og.orderid=o.id ' . ' where og.goodsid=:goodsid and  o.status>=1 and o.mid=:mid  and og.uniacid=:uniacid ', array(':goodsid' => $goods['id'], ':uniacid' => $_SESSION['uniacid'], ':mid' => $_SESSION['uid']));
		$goods['order_goodscount']=$order_goodscount;
		if ($goods['usermaxbuy'] <= $order_goodscount) {
			$goods['userbuy'] = 0;
			$goods['canbuy'] = false;
		}
	}
	$isfullback = false;
	if ($goods['isfullback']) {
		$isfullback = true;
		$fullbackgoods = pdo_fetch('SELECT * FROM ' . tablename('jy_weishop_goods_options') . ' WHERE uniacid = ' . $uniacid . ' and goodsid = ' . $id . ' limit 1 ');

		if ($goods['hasoption'] == 1) {
			$fullprice = pdo_fetch('select min(allfullbackprice) as minfullprice,max(allfullbackprice) as maxfullprice,min(allfullbackratio) as minfullratio' . "\r\n" . '                            ,max(allfullbackratio) as maxfullratio,min(fullbackprice) as minfullbackprice,max(fullbackprice) as maxfullbackprice' . "\r\n" . '                            ,min(fullbackratio) as minfullbackratio,max(fullbackratio) as maxfullbackratio,min(`day`) as minday,max(`day`) as maxday' . "\r\n" . '                            from ' . tablename('jy_weishop_goods_option') . ' where goodsid = ' . $id . '');
			$fullbackgoods['minallfullbackallprice'] = $fullprice['minfullprice'];
			$fullbackgoods['maxallfullbackallprice'] = $fullprice['maxfullprice'];
			$fullbackgoods['minallfullbackallratio'] = $fullprice['minfullratio'];
			$fullbackgoods['maxallfullbackallratio'] = $fullprice['maxfullratio'];
			$fullbackgoods['minfullbackprice'] = $fullprice['minfullbackprice'];
			$fullbackgoods['maxfullbackprice'] = $fullprice['maxfullbackprice'];
			$fullbackgoods['minfullbackratio'] = $fullprice['minfullbackratio'];
			$fullbackgoods['maxfullbackratio'] = $fullprice['maxfullbackratio'];
			$fullbackgoods['fullbackratio'] = $fullprice['minfullbackratio'];
			$fullbackgoods['fullbackprice'] = $fullprice['minfullbackprice'];
			$fullbackgoods['minday'] = $fullprice['minday'];
			$fullbackgoods['maxday'] = $fullprice['maxday'];
		}
		else{
			$fullbackgoods['maxallfullbackallprice'] = $fullbackgoods['minallfullbackallprice'];
			$fullbackgoods['maxallfullbackallratio'] = $fullbackgoods['minallfullbackallratio'];
			$fullbackgoods['minday'] = $fullbackgoods['day'];
		}
		$merchid = $goods['merchid'];
		if (json_decode($goods['labelname'], true)) {
			$labelname = json_decode($goods['labelname'], true);
		}
		 else {
			$labelname = unserialize($goods['labelname']);
		}
		if ($is_openmerch == 0) {
			if (0 < $merchid) {
				$err = true;
			}
		}
		 else if ((0 < $merchid) && ($goods['checked'] == 1)) {
			$err = true;
		}
	}
	if ((0 < $goods['isendtime']) && (0 < $goods['endtime']) && ($goods['endtime'] < time())) {
		$goods['canbuy'] = false;
	}
	$goods['timestate'] = '';
	//配送设置
	$getdispatch= model('jy_weishop/goods')->get_dispatch($goods['id']);
	$goods=array_merge($goods,$getdispatch);
	//默认已选参数：
	$pindex = max(1, 1);
	$parameter=array(
					'params'=>array('goodsid'=>$goods['id']),
					'limit'=>array($pindex,1),
					'fields'=>$fields,
					'orderby'=>'price asc',
	);
	$optionsone = pdo_getlist('jy_weishop_goods_option',$parameter);
	if($optionsone){
		$optionsid=$optionsone[0]['id'];
		$spsids=$optionsone[0]['specs'];
		//规格
		$specs_list=pdo_getall('jy_weishop_goods_spec',array('goodsid'=>$id,'uniacid'=>$_SESSION['uniacid']),array('id','title'),'','paixu asc');
		//已选规格
		$selectspecs = explode('_',$spsids);
		$selespename = '';
		if($specs_list){
				foreach($selectspecs as $spv){
					$spsitem=pdo_get('jy_weishop_goods_spec_item',array('id'=>$spv,'uniacid'=>$_SESSION['uniacid']),array('id','title'),'','paixu asc');
					if($spsitem['title']){
						$selespename .=$spsitem['title'].'，';
					}
				}
				foreach($specs_list as $k=>&$sp){
					$sp['items']=pdo_getall('jy_weishop_goods_spec_item',array('specid'=>$sp['id'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'','paixu asc');
				}
				if(count($specs_list)>0){
					$goods['optiontotal']=pdo_getsum('jy_weishop_goods_option',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$id),'stock');
					if($goods['optiontotal']<0){
						$goods['canbuy']==false;
					}else{
						$goods['canbuy']==true;
					}
				}
		}
	}
	//默认商品规格价格：
	$goods['optionid']=$optionsid;
	$goods['storeid']=$store_id;
	$goods= model('jy_weishop/activity')->get_upgoods($goods);
	$return['options']=$goods['options']?:array();
	$return['options']['thumb']=$return['options']['thumb']?tomedia($return['options']['thumb']):tomedia($goods['image']);
	$goods['image']=$return['options']['thumb']?:$goods['image'];
	$return['selespename'] = $selespename;
	$option_list=pdo_getall('jy_weishop_goods_option',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$id),array('id','specs','thumb','stock','price','productprice','costprice','goodssn','productsn','weight'),'','paixu asc');
	if($option_list){
			foreach($option_list as &$i){
				$i['specs']=explode('_',$i['specs']);
				foreach($i['specs'] as $j){
					$op=pdo_get('jy_weishop_goods_spec_item',array('id'=>intval($j)),array('specid','title'));
					$spec_title=pdo_getcolumn('jy_weishop_goods_spec',array('id'=>$op['specid']),'title');
					$i[$spec_title]=$op['title'];
				}
				$i['num']=$i['stock'];
				$i['old_price']=$i['productprice'];
				$i['cost_price']=$i['costprice'];
				$i['bm']=$i['goodssn'];
				$i['tm']=$i['productsn'];
				$i['image']=$i['thumb'];
			}
			//$specs_list['option']=$option_list;
	}
	$return['specs_list'] = $specs_list;
	
	//扩展属性
	if($goods['labelid']){
		$goods['labelname'] = pdo_getcolumn('jy_weishop_goods_label',array('id'=>$goods['labelid'],'uniacid'=>$_SESSION['uniacid']),'labelname');
		$goods['labelname'] = json_decode($goods['labelname'],true);
	}
	/*
		赠品
		先根据订单金额找赠品，再根据商品找赠品
	*/
	$goods_gifts=pdo_getall('jy_weishop_goods_gift',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'activity'=>0));
	//$current_gift=array();
	if($goods_gifts){
		foreach($goods_gifts as $gift){
			$gift['onlinetime']=json_decode($gift['onlinetime'],true);
			if($gift['onlinetime']['tstart']<time() && $gift['onlinetime']['tend']>time() && $gift['orderprice'] && $gift['orderprice']<$goods['price']){
				if(!$current_gift || $current_gift['orderprice']<$gift['orderprice']){
					$current_gift=$gift;
				}
			}
		}
	}
	if(!$current_gift){
		$goods_gifts=pdo_getall('jy_weishop_goods_gift',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'activity'=>1));
	}
	if($goods_gifts){
		foreach($goods_gifts as $gift){
			$gift['onlinetime']=json_decode($gift['onlinetime'],true);
			if($gift['onlinetime']['tstart']<time() && $gift['onlinetime']['tend']>time() && $gift['goodsid']){
					if($goods['id']==$gift['goodsid']){
						$current_gift=$gift;
						break;
					}
			}
		}
	}
	if($current_gift){
		$goods['gift_id']=$current_gift['id'];
		$gifts=json_decode($current_gift['gifts'],true);
		foreach($gifts as &$gv){
			$gv['price']=pdo_getcolumn('jy_weishop_goods',array('id'=>$gv['id'],'uniacid'=>$_SESSION['uniacid']),'price');
		}
	}
	//评价
	$comment_total=0;
	$return['list'] =  model('jy_weishop/goods')->get_comments($id,3,$comment_total);
	$return['commentsnum'] = $comment_total;
	$return['commentsfen'] =  model('jy_weishop/goods')->count_comments($id);
	$return['comments_type'] = 'all';
	//会员等级折扣
	$level = pdo_get('jy_weishop_member_level',array('id'=>$member['level'],'uniacid'=>$_SESSION['uniacid']));
	$level['discount'] = $level['discount']?$level['discount']:10;
	$member['memberprice'] =  $goods['price'] * ($level['discount']/10);
	$member['levelname'] =$level['title'];
	$return['member']=$member;
	$return['address_list'] = pdo_getall('core_address',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	if($return['address_list']){
		foreach($return['address_list'] as &$add){
			$add['pcd']=json_decode($add['pcd'],true);
		}
	}
	//可用优惠券
	$coupons= model('jy_weishop/coupon')->getmycoupon_ing(1,$goods['price'],$goods['level_de_price'],array($goods));
	//会员默认地址
	$address =  pdo_get('core_address',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid'],'isdefault'=>1));
	//收货地址：
	$address['pcd']=json_decode($address['pcd'],true);
	//$seleaddress = $address['pcd']['province'].$address['pcd']['city'].$address['pcd']['district'].$address['address'];
	$return['address'] = $address;
	//获取行政数据
	$return['province']=cfc('tools')->pcd();
	//预计送达时间
	$deliverytime = date("m月d日",strtotime("+2 day"));
	$return['deliverytime'] = $deliverytime;
	//是否可添加购物车
	$canAddCart = true;
	if (($goods['type'] == 1) || ($goods['type'] == 2) || ($goods['type'] == 3) || ($goods['type'] == 20) || !(empty($goods['cannotrefund'])) || !(empty($is_task_goods)) || !(empty($gifts))) {
		$canAddCart = false;
	}
	$cartCount = pdo_getsum('jy_weishop_member_cart', array('mid' => $_SESSION['uid'], 'uniacid' => $_SESSION['uniacid']),'total');
	$goods['isdiscount_time']=json_decode($goods['isdiscount_time'],true);
	
	if($goods['isdiscount'] && $goods['isdiscount_time']['end'] > time() && $goods['isdiscount_time']['start'] <time()){
		$goods['cuxiaoprice'] = $goods['price'];
		$return['isdiscount']=1;
	}else{
		$return['isdiscount']=0;
	}
	//商户满减
	if($saleset['enough']){
		paixu($saleset['enough'],'man','desc');
	}
	//送积分
	$buy_credit=plugin('jy_weishop')->plugin_setting('buy_credit');
	if($buy_credit['open']){
		if($buy_credit['type']==1){
			$goods['credit']=0;
			foreach($buy_credit['options'] as $buyv){
				if($buyv['start']<$goods['price']){
					$credit=$buyv['credit']*$goods['price'];
					$goods['credit']=$goods['credit']<$credit?$credit:$goods['credit'];
				}
			}
		}else{
			$goods['credit']=pdo_getcolumn('jy_weishop_goods_credit',array('goods_id'=>$goods['id'],'uniacid'=>$_SESSION['uniacid']),'credit');
		}
		paixu($saleset['enough'],'man','desc');
	}
	//积分抵扣
	if($saleset['deduct_credit1']==1){
		$goods['deduct_credit']=$member['credit1']/$saleset['deduct_bili'];
		if($goods['deduct_credit']>$goods['price']/2){
			$goods['deduct_credit']=price($goods['price']/2,true);
		}
	}
	//满减、包邮活动
	if(($goods['type'] !=1 && $goods['isverify'] !=1 && $goods['dispatchtype'] ==1 && $goods['dispatchprice']==0) || $saleset['baoyou_status']==1 || $goods['fullnum']>0 || $goods['fullmoney']>0 ){
		$return['manjian']=1;	
	}
	//抵扣、赠送积分活动
	if(($goods['deduct_credit'] && $goods['deduct_credit'] !='0.00') || $goods['credit']){
		$return['deduct_credit']=1;	
	}
	if($saleset['enough'] || $return['manjian'] || $return['deduct_credit']){
		$return['huodong']=1;
	}else{
		$return['huodong']=0;
	}
	if((!empty($coupons) || $return['isdiscount'] || $return['huodong'] || $credit['buy'] || $goods['buyagain']>0  || ($fullbackgoods && $isfullback))){
		$return['sale']=1;
	}else{
		$return['sale']=0;
	
	}
	//热销商品（按笔数）
	$parameter=array(
			'params'=>array('uniacid'=>$_SESSION['uniacid'],'status'=>'1'),
			'fields'=>array('id','title','image','price','total','salerealnum','salenum'),
			'orderby'=>'salerealnum desc',
			'limit'=>'6',
	);
	$hot_list=pdo_getlist('jy_weishop_goods',$parameter);
	foreach($hot_list as &$hv){
		$hv['salenum']=$hv['salerealnum']+$hv['salenum'];
	}
	paixu($hot_list,'salenum desc');
	$return['hot_list']=$hot_list;
	//看了又看（按时间）
	$parameter=array(
			'params'=>array('uniacid'=>$_SESSION['uniacid']),
			'orderby'=>'createtime desc',
			'limit'=>'6',
	);
	$history=pdo_getlist('jy_weishop_member_history',$parameter);
	foreach($history as $ke=>&$hiv){
		$hiv['goodsinfo']=pdo_get('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'status'=>'1','id'=>$hiv['goodsid']),array('id','title','image','price','total','salerealnum','salenum'));
		if(!$hiv['goodsinfo']){
			unset($history[$ke]);
		}
	}
	$return['history']=$history;
	//商品描述为空
	//去掉多余空样式
	$goods['content']=str_replace('style=""','',$goods['content']);
	$goods['content']=$goods['content']?$goods['content']:'<div></div>';
	//参数
	$goods['param']=json_decode($goods['param'],true);
	//问答
	$wenda = array();
	$return['wenda']=$wenda;
	//货物地址：
	$goods['pcd'] = json_decode($goods['pcd'],true);
	//小程序二维码：
	$url='/jy_pingdd_pin/goods/detail/index?id='.$_SESSION['goodsid'].'&fromuser='.$_SESSION['uid'];
	$pid='wxappnew'.$_SESSION['wxapp_id'].'_'.$_SESSION['goodsid'];
	if($_SESSION['fromh5wxapp']){
		$h5url='jy_pingdd_pin/goods.detail//id='.$_SESSION['goodsid'].'&fromuser='.$_SESSION['uid'];
		$url='/pages/index/index?url='.urlencode($h5url);
	}
	$goods['appcode'] =  model('jy_weishop/model')->get_wxappewm($url,$pid);
	$goods['subtitle1']=cfc('tools')->substr($goods['subtitle1'],0,13);
	$goods['title1']=cfc('tools')->substr($goods['title'],0,14);
	$goods['title2']=cfc('tools')->substr($goods['title'],14,13);
	$goods['title2']=$goods['subtitle1']?$goods['subtitle1']:$goods['title2'];
	$goods['title2']=$goods['title2']?$goods['title2'].'...':'暂无描述';
	//海报文件
	if($goods['isposter']){
		if($_SESSION['fromh5wxapp'] || $_SESSION['container']=='wxapp'){
			$poster=model('jy_poster/model')->get_active(3,'',$_SESSION['goodsid'],$_SESSION['openid'],$goods['appcode'],'','jy_pingdd_pin');
		}else{
			$poster=model('jy_poster/model')->get_active(3,'',$_SESSION['goodsid'],$_SESSION['openid'],'','','jy_pingdd_pin');
		}
	}
	//判断nobuy状态：
	if($goods['userbuy']==0){
		$goods['nobuytitle']='您已经超出最大'.$goods['usermaxbuy'].'件购买量';
	}elseif($goods['levelbuy']==0){
		$goods['nobuytitle']='您当前会员等级没有购买权限';
	}elseif($goods['groupbuy']==0){
		$goods['nobuytitle']='您所在的用户组没有购买权限';
	}elseif($goods['timebuy']==-1){
		$goods['nobuytitle']='未到开始抢购时间!';
	}elseif($goods['timebuy']==1){
		$goods['nobuytitle']='抢购时间已经结束';
	}else{
		$goods['nobuytitle']='商品已售罄';
	}
	//POST提交：
	$post = array();
	if($_SESSION['current_plugin']=='jy_weidish' || $_SESSION['current_plugin']=='jy_weishoppei' || $_SESSION['current_plugin']=='jy_weishoppeishop' || $_SESSION['current_plugin']=='jy_weishopscale'){
		$post['isdish'] = true;
	}
	$post['num'] = 1;
	$post['goodsid'] = $id;
	$post['specid']=$selectspecs;
	$post['addressid']=0;
	$post['optionsid']=$return['options']['id']?:0;
	$post['storeid']=0;
	$post['teamid']=0;
	$return['isvideo']=1;
	$return['post']=$post;
	$goods['url']=custom_url('jy_pingdd_pin/goods.detail//id='.$goods['id'],true);
	$return['posterimg']=$poster['poster'];
	$return['canAddCart']=$canAddCart;
	$return['cartCount']=$cartCount;
	$return['coupons']=$coupons;
	$return['gifts']=$gifts;
	$return['isgift']=$isgift;
	$return['has_city']=$goods['has_city'];
	$return['citys']=$goods['citys'];
	$return['dispatch']=$getdispatch;
	$return['spec_titles']=$spec_titles;
	$return['shopdetail']=$shopdetail;
	$return['fullbackgoods']=$fullbackgoods;
	$return['images']=$goods['images'];
	$return['saleset']=$saleset;
	$return['showtype']=0;
	//好物圈
	$install = check_account_permission('jy_hwq');
	$return['jy_hwqset']=array();
	if($install){
		$return['jy_hwqset']=plugin('jy_hwq')->plugin_setting('basic');
	}
	$return['jy_hwqset']=$return['jy_hwqset']?:array();
	//现在时间时间戳
	$return['time']=time();
	$date_time=date('Y-m-d H:i:s',$return['time']);
	$hasServices = $goods['cash'] || $goods['seven'] || $goods['repair'] || $goods['invoice'] || $goods['quality'];
	$return['hasServices']=$hasServices;
	$return['goodsurl']=custom_url('jy_pingdd_pin/goods.detail//id='.$id,true);
	//set_html('title',$goods['title']);
	$return['sharedata']=array(
		'title'=>$goods['title'],
		'desc'=>$goods['subtitle']?$goods['subtitle']:$goods['title'],
		'link'=>custom_url('jy_pingdd_pin/goods.detail//id='.$id.'&fromuser='.$_SESSION['uid'].'&current_plugin='.$_SESSION['current_plugin'],true),
		'imgUrl'=>tomedia($goods['image'])
	);
	//如果是朋友圈商城代理，调整分享连接地址
	if($goods['active']['kfewm']){
			$return['kfewm']=$goods['active']['kfewm'];
	}
	if($goods['active']['sharedata']){
			$return['sharedata']=array_merge($return['sharedata'],$goods['active']['sharedata']);
	}
	if(empty($return['goods']['active'])){
		$return['goods']['active']= model('jy_weishop/activity')->get_sales($goods['id']);
		if($return['goods']['active']['discount']>10){
					$return['goods']['oldprice']=$return['goods']['price'];
					$return['goods']['price']=$return['goods']['oldprice']*$return['goods']['active']['discount']/100;
		}
	}
	$fans=model('core/member')->detail($_SESSION['uid'],array('credit1','credit2','mobile','avatar','nickname'),true);
	$fans['time']=date('Y-m-d H:i:s',TIMESTAMP);
	$fans['nickname']=$fans['nickname']?$fans['nickname']:'匿名';
	$return['member']=array_merge($fans,$return['member']);
	$return['item']['price']=price($return['item']['price']);
	//小程序直播设置
	$sale_live=plugin('jy_weishop')->plugin_setting('sale_live');
	if($sale_live['isopen']){
		$return['roomsid']=pdo_getcolumn('core_account_wxapp_live_rooms',array('goodsid'=>$goods['id'],'uniacid'=>$_SESSION['uniacid']),'roomid');
	}
	if(!$post['isdish'] && !$return['options']['id'] && (!$return['seckillinfo'] ||  $return['seckillinfo']['status']!=1) && $goods['canbuy']){
		$return['gobuy']=1;
	}
	if(!$post['isdish'] && $return['options']['id'] && (!$return['seckillinfo'] ||  $return['seckillinfo']['status']!=1) && $goods['canbuy']){
		$return['gobuy']=2;
	}
	//拼团部分：
	$goods=$this->model('model')->up_price($goods,$return['pluginset'],true);
	if($return['pluginset']['goodsdesc1']){
		$goodsdesc1=$return['pluginset']['goodsdesc1'];
	}else{
		$goodsdesc1='{teamnum}人成团,随机抽{lastnum}人拼中,没抽中的人获得{fan_fee}元奖励金！';
	}
	$goodsdesc1=str_replace('{teamnum}',$goods['teamnum'],$goodsdesc1);
	$goodsdesc1=str_replace('{fan_fee}',$goods['fan_fee'],$goodsdesc1);
	$goodsdesc1=str_replace('{fannum}',$goods['failnum'],$goodsdesc1);
	$goodsdesc1=str_replace('{zhong_fee}',$goods['zhong_fee'],$goodsdesc1);
	$goods['goodsdesc1']=str_replace('{lastnum}',$goods['lastnum'],$goodsdesc1);
	//拼团时间
	$return['times']=$this->model('model')->get_times($pinggoods['timeids']);
	$return['canping']=$this->model('model')->get_times($pinggoods['timeids'],'',true);
	if(!$canping && $goods['canbuy']){
		$goods['nobuytitle']='拼团还未开始';
	}
	if($pinggoods['endtime'] && $pinggoods['endtime']<TIMESTAMP){
		$return['canping']=false;
		$goods['nobuytitle']='拼团已结束';
	}
	//判断是否预约：
	$return['isyuyue']=pdo_getcolumn('jy_pingdd_pin_yuyue',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'pingid'=>$_SESSION['pinggoodsid']),'id');
	$return['isyuyue']=$return['isyuyue']?1:0;
	$return['goods']=$goods;
	$return['teamsnum']=pdo_count('jy_weishop_groups_team',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$goods['id'],'status'=>0),'id');
	set_html('title',$goods['title']?:'商品详情');
	exi($return);
}

if($_RQ['op']=='addtocart'){
	$post=get_postdata();
	$id=intval($post['goodsid']);
	$number=intval($post['num']);
	if(!$id){
		exi('选择的商品不存在，请重新选择！','error');
	}
	if(!$number){
		$number=1;
	}
	$spsids = '';
	if($post['specid']){
		foreach($post['specid'] as $k=>&$s){
			if($k==0){
				$spsids .= $s;
			}else{
				$spsids .= '_'.$s;
			}
		}
	}
	$optionid = pdo_getcolumn('jy_weishop_goods_option',array('goodsid'=>$id,'specs'=>$spsids),'id');
	$goods=pdo_get('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	$ifgoods=pdo_get('jy_weishop_member_cart',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'goodsid'=>$id,'optionid'=>$optionid));
	$data=array(
		'uniacid'=>$_SESSION['uniacid'],
		'mid'=>$_SESSION['uid'],
		'goodsid'=>$id,
		'price'=>$goods['price'],
		'optionid'=>$optionid,
	);
	if($ifgoods){
		$data['total']=$ifgoods['total']+$number;
		 model('jy_weishop/goods')->check_buy($data['goodsid'],intval($data['total']),intval($data['optionid']),true,true);
		pdo_update('jy_weishop_member_cart',$data,array('uniacid'=>$_SESSION['uniacid'],'id'=>$ifgoods['id']));
	}else{
		$data['total']=$number;
		 model('jy_weishop/goods')->check_buy($data['goodsid'],intval($data['total']),intval($data['optionid']),true,true);
		//如果来自其他商户分销（保存商户ID）
		$data['total']['merchid']=$this->session('frommerchid');
		pdo_insert('jy_weishop_member_cart',$data);
	}
	exi('成功加入购物车！');
}
if($_RQ['op']=='get_optionsid'){
	$post=get_postdata();
	$id=$post['goodsid']?:$_SESSION['goodsid'];
	$store_id=$this->session('store_id');
	if(!$id){
		exi('请先选择商品！','error');
	}
	if($post['storeid']){
		
	}
	//规格
	$specids=explode('_',$post['specid']);
	
	//已选规格
	$selespename = '';
	if($specids){
			foreach($specids as $k=>$sp){
				$spec_item=pdo_get('jy_weishop_goods_spec_item',array('id'=>$sp,'uniacid'=>$_SESSION['uniacid']),array('id','title'));
				$selespename .=$spec_item['title'].'，';
			}
	}
	$optionsid=pdo_getcolumn('jy_weishop_goods_option',array('goodsid'=>$id,'specs'=>$post['specid']),'id');
	//默认商品规格价格：
	$goods=pdo_get('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	$goods['optionid']=$optionsid;
	$goods['storeid']=$store_id;
	$goods= model('jy_weishop/activity')->get_upgoods($goods);
	$return['options']=$goods['options'];
	$return['options']['thumb']=$return['options']['thumb']?tomedia($return['options']['thumb']):tomedia($goods['image']);
	if($post['isdish']){
		$return['carttotal']=pdo_getcolumn('jy_weishop_member_cart',array('selected'=>1,'optionid'=>$return['options']['id'],'goodsid'=>$id,'mid'=> $_SESSION['uid'], 'uniacid' => $_SESSION['uniacid']),'total');
	}else{
		$return['carttotal']=pdo_getcolumn('jy_weishop_member_cart',array('optionid'=>$return['options']['id'],'goodsid'=>$id,'mid'=> $_SESSION['uid'], 'uniacid' => $_SESSION['uniacid']),'total');
	}
	$return['carttotal']=$return['carttotal']?:0;
	$return['selespename'] = $selespename;
	exi($return);
}
if($_RQ['op']=='change_address'){
	$post=get_postdata();
	$addressid = intval($post['addressid']);
	$id=$_SESSION['goodsid'];
	if(!$id){
		exi('请先选择商品！','error');
	}
	$address =  pdo_get('core_address',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid'],'id'=>$addressid));
	$address['pcd']=json_decode($address['pcd'],true);
	$return['address'] =$address;
	exi($return);
}
if($_RQ['op']=='like'){
	$id = $_SESSION['goodsid'];
	if(!$id){
		$post=get_postdata();
	}
	$like=pdo_get('jy_weishop_member_like',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'goodsid'=>$id),'id');
	if($like){
			pdo_delete('jy_weishop_member_like',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'goodsid'=>$id));
			exi('取消成功');
	}else{
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'goodsid'=>$id,
			'createtime'=>TIMESTAMP
		);
		pdo_insert('jy_weishop_member_like',$insert);
		exi('关注成功');
	}
	
}
if($_RQ['op']=='yuyue'){
	$post=get_postdata();
	$id = $_SESSION['pinggoodsid'];
	if(!$id){
		$post=get_postdata();
	}
	$yuyue=pdo_get('jy_pingdd_pin_yuyue',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'pingid'=>$id),'id');
	if($yuyue){
			pdo_delete('jy_pingdd_pin_yuyue',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'pingid'=>$id));
			exi('已取消预约拼团！');
	}else{
		$addressid=$post['addressid'];
		$this->model('model')->set_yuyue($id,$addressid);
		exi('success');
	}
}
if($_RQ['op']=='check_buy'){
	$goodsid=$_RQ['goodsid']?intval($_RQ['goodsid']):$_SESSION['goodsid'];
	model('jy_weishop/goods')->check_buy($goodsid,$_RQ['num'],$_RQ['optionsid']);
	if($_RQ['single']){
		exi('','','jy_weishop/order.create//goodsid='.$goodsid.'&num='.$_RQ['num'].'&optionsid='.$_RQ['optionsid'].'&addressid='.$_RQ['addressid'].'&storeid='.$_RQ['storeid'].'&roomid='.$_RQ['roomid']);
	}else{
		$this->model('model')->checkteam($goodsid);
		exi('','','jy_pingdd_pin/order.create//teamid='.$_RQ['teamid'].'&goodsid='.$goodsid.'&num='.$_RQ['num'].'&optionsid='.$_RQ['optionsid'].'&addressid='.$_RQ['addressid'].'&storeid='.$_RQ['storeid'].'&roomid='.$_RQ['roomid']);
	}
}
if($_RQ['op']=='getcomments'){
	$goodsid=$_RQ['goodsid']?intval($_RQ['goodsid']):$_SESSION['goodsid'];
	$return['list']= model('jy_weishop/goods')->get_comments($goodsid,6,$comment_total,$_RQ['type'],$_RQ['page']);
	exi($return);
}
if($_RQ['op']=='getactive'){
	$goods_id=intval($_RQ['goods_id']);
	$active_id=intval($_RQ['active_id']);
	$active_plugin=$_RQ['active_plugin'];
	if($active_plugin && $goods_id){
		$active= model('jy_weishop/activity')->get_active($goods_id,$active_id,$active_plugin);
	}
	$return['active']=$active;
	exi($return);
}